<template>
  <div id="record">
    <h2>点击记录</h2>
    <div class="record-container">
      <div class="record-actions">
        <button @click="createTestRecord" :disabled="loading">
          {{ loading ? "加载中..." : "创建测试记录" }}
        </button>
        <button @click="clearRecords" :disabled="loading">清空记录</button>
      </div>
      <div v-if="error" class="error-message">
        {{ error }}
      </div>

      <div class="record-list" v-if="records.length > 0">
        <table>
          <thead>
            <tr>
              <th>时间</th>
              <th>IP地址</th>
              <th>国家/地区</th>
              <th>设备类型</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(record, index) in records" :key="index">
              <td>{{ formatDate(record.timestamp) }}</td>
              <td>{{ record.ip }}</td>
              <td>{{ record.country }}/{{ record.regionName }}</td>
              <td>{{ record.deviceType }}</td>
            </tr>
          </tbody>
        </table>
      </div>
      <div class="no-records" v-else>暂无记录数据</div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { useIpApi } from "@/services/ipApi";

interface Record {
  timestamp: number;
  ip: string;
  country: string;
  regionName: string;
  deviceType: string;
}

const records = ref<Record[]>([]);
const loading = ref(false);
const error = ref("");
const { getIpInfo } = useIpApi();

// 从本地存储加载记录
onMounted(() => {
  const savedRecords = localStorage.getItem("clickRecords");
  if (savedRecords) {
    records.value = JSON.parse(savedRecords);
  }
});

// 保存记录到本地存储
function saveRecords() {
  localStorage.setItem("clickRecords", JSON.stringify(records.value));
  console.log(JSON.stringify(records.value));
}

// 创建新记录
async function createRecord(ip?: string) {
  loading.value = true;
  error.value = "";
  try {
    // 如果没有提供IP，则使用API获取
    const ipInfo = await getIpInfo(ip);

    const newRecord: Record = {
      timestamp: Date.now(),
      ip: ipInfo.ip,
      country: ipInfo.country,
      regionName: ipInfo.regionName,
      deviceType: ipInfo.deviceType,
    };

    records.value.unshift(newRecord);
    saveRecords();
    return newRecord;
  } catch (error) {
    console.error("创建记录失败:", error);
    error.value = "获取IP信息失败，请稍后重试";
    return null;
  } finally {
    loading.value = false;
  }
}

// 创建测试记录按钮
async function createTestRecord() {
  await createRecord();
}

// 清空记录
function clearRecords() {
  records.value = [];
  saveRecords();
}

// 格式化日期
function formatDate(timestamp: number): string {
  const date = new Date(timestamp);
  return date.toLocaleString();
}

// 导出方法供其他组件使用
defineExpose({
  createRecord,
});
</script>

<style scoped>
#record {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.record-container {
  margin-top: 20px;
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.record-actions {
  margin-bottom: 20px;
  display: flex;
  gap: 10px;
}

button {
  padding: 8px 16px;
  background-color: #4caf50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #45a049;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  border: 1px solid #ddd;
  padding: 12px;
  text-align: left;
}

th {
  background-color: #f2f2f2;
}

tr:nth-child(even) {
  background-color: #f9f9f9;
}

tr:hover {
  background-color: #f1f1f1;
}

.no-records {
  text-align: center;
  padding: 40px;
  color: #999;
  font-style: italic;
}

.error-message {
  margin: 10px 0;
  padding: 10px;
  background-color: #ffebee;
  color: #c62828;
  border-radius: 4px;
  text-align: center;
}
</style>
